package cn.gaozhong.org.service.impl;

import cn.gaozhong.basic.exception.CustomException;
import cn.gaozhong.basic.util.BaiduAuditUtils;
import cn.gaozhong.org.domain.Employee;
import cn.gaozhong.org.domain.Shop;
import cn.gaozhong.org.domain.ShopAuditLog;
import cn.gaozhong.org.mapper.EmployeeMapper;
import cn.gaozhong.org.mapper.ShopAuditLogMapper;
import cn.gaozhong.org.mapper.ShopMapper;
import cn.gaozhong.org.service.IShopService;
import cn.gaozhong.basic.service.impl.BaseServiceImpl;

import cn.gaozhong.org.vo.ShopVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author gaozhong
 * @since 2022-06-12
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;
    @Autowired
    private JavaMailSender javaMailSender;
    /**
     * 店铺入驻逻辑
     *  一：参数空校验
     *    。。不能为空
     *  二：合法校验   @TODO 百度AI智能审核接口做
     *
     *  三：判断是否曾经已经注册过
     *    1. 店铺不能被注册
     *    2.人是否被注册
     *      2.1 注册了但是没有绑定店铺
     *      2.2 注册了并且已经绑定了店铺
     *
     *  四：保存数据
     *    shop
     *    employee  shopId
     *
     *    反过来修改shop中的admin_id
     * @param shop
     *
     */
    @Override
    public void settlement(Shop shop) {
        Employee admin = shop.getAdmin();
        //判断参数不能为空
        if (StringUtils.isEmpty(shop.getName())
                ||StringUtils.isEmpty(shop.getTel())
                ||StringUtils.isEmpty(shop.getAddress())
                ||StringUtils.isEmpty(shop.getAdmin())){
            throw new CustomException("表格不能为空，请重新填写");
        }
        if (!BaiduAuditUtils.TextCensor(shop.getName())){
            throw new CustomException("嘿！不要搞事情哦");
        }
        //根据name查询数据
        Shop name = shopMapper.loadByName(shop.getName());

        //根据username查询数据
        Employee emp = employeeMapper.loadByUsername(admin.getUsername());
        if(null == emp){
            //新增店铺名
            employeeMapper.save(admin);
            emp = admin;
        }
        //判断店铺是否存在
        if (name==null){
            //给店铺负责人id赋值为员工id
            shop.setAdminId(emp.getId());
            //新增店铺入驻数据
            shopMapper.save(shop);
        }else{
            throw new CustomException("店铺已被注册，请直接登录");
        }

        if (emp!=null&&emp.getShop_id()==null){
            //给员工Shop_id赋值为店铺id;
            emp.setShop_id(shop.getId());
            //给员工加上Shop_id;
            employeeMapper.update(emp);
        }else{
            throw new CustomException("姓名已被注册过，请重新输入负责人");

        }




    }

    /**
     * 审核通过
     *
     * @param log
     */
    @Override
    public void pass(ShopAuditLog log) {
        //1.修改店铺状态为---审核通过
        Shop shop = shopMapper.loadById(log.getShopId());
        shop.setState(1);
        shopMapper.update(shop);
        //2.记录审核通过日志
        log.setAuditId(333L);
        shopAuditLogMapper.save(log);
        //3发送激活连接邮件
        Employee employee = employeeMapper.loadByShopId(log.getShopId());
        SimpleMailMessage message = new SimpleMailMessage();
        //设置发送人
        message.setFrom("1793057582@qq.com");
        //邮件主题
        message.setSubject("审核");
        //邮件内容
        message.setText("审核通过，请点击连接去激活店铺：");
        //收件人
        message.setTo("1793057582@qq.com");
        System.out.println(employee.getEmail());
        javaMailSender.send(message);
    }

    /**
     * 驳回
     * @param log
     */
    @Override
    public void reject(ShopAuditLog log) {
        //1.修改店铺状态为--驳回
        Shop shop = shopMapper.loadById(log.getShopId());
        shop.setState(-1);
        shopMapper.update(shop);
        //2.记录审核通过日志
        log.setAuditId(333L);
        shopAuditLogMapper.save(log);
        //3发送激活连接邮件
        Employee employee = employeeMapper.loadByShopId(log.getShopId());
        SimpleMailMessage message = new SimpleMailMessage();
        //设置发送人
        message.setFrom("1793057582@qq.com");
        //邮件主题
        message.setSubject("审核");
        //邮件内容
        message.setText("审核失败，请点击连接去修改你的内容：");
        //收件人
        message.setTo("1793057582@qq.com");


        javaMailSender.send(message);
    }

    @Override
    public void batchSave(List<Shop> shops) {
        shopMapper.batchSave(shops);
    }

    @Override
    public List<ShopVo> queryEcharts() {
        return shopMapper.queryEcharts();
    }
}
